package org.gushi.algorithm.a01;

/**
 * 冒泡排序算法
 */
public class BubbleSort<E extends Comparable<E>> {

    private E[] list;


    public BubbleSort(E[] list) {
        this.list = list;
    }

    public E[] listSort(){
       return listSort("ASC");
    }
    /**
     * 冒泡排序算法
     * O(n^2)
     * @return
     */
    public E[] listSort(String orderType){
        for (int i = 0; i<list.length;i++) {
            for (int j = i+1; j<list.length;j++){
                if(orderType.equals("DESC") || orderType.equals("desc")){
                    if(list[i].compareTo(list[j])<0){
                        E e2 = list[i];
                        list[i] = list[j];
                        list[j] = e2;
                    }
                }else{
                    if(list[i].compareTo(list[j])>0){
                        E e2 = list[i];
                        list[i] = list[j];
                        list[j] = e2;
                    }
                }
            }
        }
        return list;
    }


}
